package com.eascs.web.o2o.web.reward.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.o2o.web.reward.entity.OverdualForm;
import com.eascs.web.o2o.web.reward.service.CmRdRelationService;
import com.eascs.web.o2o.web.reward.service.CurLoanDetailService;
import com.eascs.web.o2o.web.reward.service.OverdualReportService;
import com.eascs.web.o2o.web.reward.service.ProvinceOverdueService;
import com.eascs.web.o2o.web.reward.service.ReportForLoanDetailService;
import com.eascs.web.o2o.web.reward.service.ValidLoanService;
import com.eascs.web.o2o.web.util.ExcelParser;
import com.eascs.web.o2o.web.util.ExcelType;

@RestController
@RequestMapping("/overdualRest")
public class OverdualReportRestController {

	private static Logger log = LoggerFactory.getLogger(OverdualReportRestController.class);

	@Autowired
	OverdualReportService overdualReportService;
	
	@Autowired
	ReportForLoanDetailService reportForLoanDetailService;
	
	@Autowired
	CmRdRelationService cmRdRelationService;
	
	@Autowired
	CurLoanDetailService curLoanDetailService;

	@Autowired
	ProvinceOverdueService provinceOverdueService;
	
	@Autowired
	ValidLoanService validLoanService;
  	
  	 //根据省区查询逾期报表
  	@RequestMapping("/overdualReportDetail")
  	public PaginationRestResult overdualReportDetail(OverdualForm entity) {
  		String o2ourl = "/overdualRest/overdualReportDetail";
  		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
  		if (entity != null) {
  			 String province = entity.getProvince() == null ? "" : entity.getProvince();
  			 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
			 String loanDateS = entity.getLoanDateS()== null ? "" : entity.getLoanDateS();
			 String loanDateE= entity.getLoanDateE()== null ? "" : entity.getLoanDateE();
			 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
			 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
			 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateS", loanDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateE", loanDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = overdualReportService.getOverdualReportDetail(entity);
  		String titles[] = { "考核月份", "省区", "单号","借款人姓名", "经办人名称", "放款金额(万元)", "放款期限", "放款时间","逾期时间","逾期本金(万元)","逾期利息(万元)","逾期类型","逾期罚息(万元)","逾期总额(万元)"};
  		SelectDataOption[] selectDataOptions = { null, null,null,null, null, null, null, null, null, null, null,null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(true);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		//o2oPagingInfo.setFormats(new Format[] { null, null, new DecimalFormat(",##0.00"), null, new DecimalFormat(",##0.00"), null,  new DecimalFormat(",##0.00"),null, new DecimalFormat(",##0.00"), null , new DecimalFormat(",##0.00"), null , new DecimalFormat(",##0.00")});
  		o2oPagingInfo.setRendTo("#o2o_result");
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
	

	
	
	//根据省区查询逾期报表
  	@RequestMapping("/receivedPaymentsDetail")
  	public PaginationRestResult receivedPaymentsDetail(OverdualForm entity) {
  		String o2ourl = "/overdualRest/receivedPaymentsDetail";
  		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
  		if (entity != null) {
  			 String province = entity.getProvince() == null ? "" : entity.getProvince();
			 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
			 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
			 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
			 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = overdualReportService.getReceivedPaymentsDetail(entity);
  		String titles[] = { "考核月份", "省区", "单号","借款人姓名", "经办人名称", "放款金额(万元)", "放款期限", "放款时间","逾期时间","回款金额(万元)","回款时间","清收人","清收系数","回款类型"};
  		SelectDataOption[] selectDataOptions = { null, null,null, null,null, null, null, null, null, null, null,null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(true);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		//o2oPagingInfo.setFormats(new Format[] { null, null, new DecimalFormat(",##0.00"), null, new DecimalFormat(",##0.00"), null,  new DecimalFormat(",##0.00"),null, new DecimalFormat(",##0.00"), null , new DecimalFormat(",##0.00"), null , new DecimalFormat(",##0.00")});
  		o2oPagingInfo.setRendTo("#o2o_result");
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
	
  	
	//根据省区查询逾期报表
  	@RequestMapping("/receiveOverdueAmount")
  	public PaginationRestResult receiveOverdueAmount(OverdualForm entity) {
  		String o2ourl = "/overdualRest/receiveOverdueAmount";
  		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
  		if (entity != null) {
  			 String province = entity.getProvince() == null ? "" : entity.getProvince();
			 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
			 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
			 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
			 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = overdualReportService.getReceiveOverdueAmount(entity);
  		String titles[] = { "考核月份", "省区", "被考核对象工号", "被考核对象", "贷款单号","借款人姓名", "放款金额(万元)", "贷款期限","折算后放款金额(万元)","放款日期","到期日期","回款金额(万元)","回款时间","清收人","清收系数","回款类型","被考核对象与本单的关系"};
  		SelectDataOption[] selectDataOptions = { null, null,null, null, null, null, null, null, null, null,null, null, null,null, null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(true);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		o2oPagingInfo.setRendTo("#o2o_result");
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
	
  	
	//根据省区查询逾期报表
  	@RequestMapping("/curOverdueAmount")
  	public PaginationRestResult curOverdueAmount(OverdualForm entity) {
  		String o2ourl = "/overdualRest/curOverdueAmount";
  		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
  		if (entity != null) {
  			 String province = entity.getProvince() == null ? "" : entity.getProvince();
			 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
			 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
			 String loanStatus= entity.getLoanStatus()== null ? "" : entity.getLoanStatus();
			 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "loanStatus", loanStatus);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = overdualReportService.getCurOverdueAmount(entity);
  		String titles[] = { "考核月份", "省区", "被考核对象工号", "被考核对象", "贷款单号","借款人姓名", "放款金额(万元)", "贷款期限","折算后放款金额(万元)","放款日期","到期日期","逾期发生时间","逾期本金(万元)","逾期利息(万元)","逾期罚息(万元)","逾期总额(万元)","逾期类型","被考核对象与本单的关系"};
  		SelectDataOption[] selectDataOptions = { null, null,null, null, null, null, null, null, null, null,null, null, null,null, null, null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(true);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		o2oPagingInfo.setRendTo("#o2o_result");
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
	
	//根据省区查询逾期报表
  	@RequestMapping("/rewardSalaryCheck")
  	public PaginationRestResult rewardSalaryCheck(OverdualForm entity) {
  		String o2ourl = "/overdualRest/rewardSalaryCheck";
  		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getMonth()==null?date:entity.getMonth();
		entity.setMonth(month);
  		if (entity != null) {
  			 String province = entity.getProvince() == null ? "" : entity.getProvince();
			 String monthData = entity.getMonth() == null ? "" : entity.getMonth();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "month", monthData);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
  		} else {
  			o2ourl = o2ourl + "?o2oIndex=";
  		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = overdualReportService.getRewardSalaryCheck(entity);
  		String titles[] = { "考核月份", "省区", "被考核对象工号", "被考核对象", "岗位","考核类型", "入职时间", "月度基础工资","绩效考核系数","有效投放总额(万元)","激励方案","提成系数","起提点","月度业绩提成(万元)","实际投放总额(万元)","当月风险金计提金额(万元)","当月风险金发放金额(万元)","核算金额"};
  		SelectDataOption[] selectDataOptions = { null, null,null, null, null, null, null, null, null, null,null, null, null,null, null, null, null, null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(true);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		o2oPagingInfo.setRendTo("#o2o_result");
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
  	
  	
  	
  	
  	
  //根据省区查询逾期报表
  	@RequestMapping("/evaluateLoanDay")
  	public PaginationRestResult evaluateLoanDay(OverdualForm entity) {
  		String o2ourl = "/overdualRest/evaluateLoanDay";
  		if (entity != null) {
			 String province = entity.getProvince() == null ? "" : entity.getProvince();
			 String overDateS = entity.getOverDateS()== null ? "" : entity.getOverDateS();
			 String overDateE= entity.getOverDateE()== null ? "" : entity.getOverDateE();
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateS", overDateS);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "overDateE", overDateE);
			 o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			 o2ourl = o2ourl + "&o2oIndex=";
		} else {
			 o2ourl = o2ourl + "?o2oIndex=";
		}
  		entity.setPageSize(10000);
  		PageResult<Object[]> pageResult = overdualReportService.evaluateLoanDay(entity);
  		String titles[] = {  "省区", " 放款所需天数", "放款所需天数分数"};
  		SelectDataOption[] selectDataOptions = { null, null,null};
  		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
  		o2oPagingInfo.setBuildPagingNavigation(true);
  		o2oPagingInfo.setFirstRenderColumnIndex(0);
  		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
  		o2oPagingInfo.setRendTo("#o2o_result");
  		ViewContext.put("vo", entity);
  		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
  	}
  	
  	
  	

  	
  	@RequestMapping("/toImport")
	public HtmlRestResult toImport(String type) {
  		ViewContext.put("type", type);
		return HtmlRestResult.popHtmlFragment("overdue/import_overdueReport");
	}

	//数据导入
  	@RequestMapping("/uploadExecl")
	public BaseRestResult<String> uploadExcel(@RequestParam(value = "file", required = false) MultipartFile file, String downtype) {
		@SuppressWarnings("unused")
		InputStream inputStream = null;
		try {
			inputStream = file.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//设置单元格的校验
		ExcelType[] types = setExcel(downtype);
		
		long beginTime = System.currentTimeMillis();
		ExcelParser parser = new ExcelParser(types);
		try {
			parser.run(file.getInputStream());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		List<Object> errorresults = parser.getErrorresults();
		StringBuffer error = new StringBuffer();
		for (int i = 0; i < errorresults.size(); i++) { //如果数据格式有问题，先打出loger 
			error.append("导入数据有误请检查：错误数据位置为："+(String) errorresults.get(i)+"\r\n");
			log.error("error.excel.read", (String) errorresults.get(i));
		}
		if(error!=null&&StringUtils.isNotBlank(error)){
			return BaseRestResult.warn("导入数据格式有误，请检查导入报表数据。导入报表模板可从系统导出");
		}
		
		 //解析出来的数据
		List<List<Object>> results = parser.getResults(); 
		//循环去查询表插入逾期报表
		
		log.info(new StringBuilder().append("耗时：").append(
				(System.currentTimeMillis() - beginTime)).append(
				",导入记录数：").append(results.size()).append(
				",批量导入级别-导入Excel").toString());
		try {
			//保存解析出来的数据
			if("overdue".equals(downtype)){
				overdualReportService.addList(results);
			}else if("payments".equals(downtype)){
				overdualReportService.addListByReceivedPaymentsDetail(results);
			}else if("loanDetail".equals(downtype)){
				reportForLoanDetailService.addLoanDeatilList(results);
			}else if("cmRdRelation".equals(downtype)){
				cmRdRelationService.addCmRdRelationList(results);
            }else if("curLoanDetail".equals(downtype)){
				curLoanDetailService.addCurLoanDetailList(results);
			}else if("provinceOverdue".equals(downtype)){
				provinceOverdueService.addProvinceOverdueList(results);
            }else if("curOverdue".equals(downtype)){//当期逾期本息金额报表
		    	overdualReportService.addListByCurOverdueAmount(results);
		    }else if("receiveOverdue".equals(downtype)){//当期回款本息金额报表
		    	overdualReportService.addListByReceiveOverdueAmount(results);
		    }else if("validLoan".equals(downtype)){//有效投放总额报表
		    	validLoanService.addValidLoanList(results);
		    }	
			
		} catch (RuntimeException e) {
			e.getMessage();
			log.error("导入数据失败 , downtype:{}", downtype ,e);
			return BaseRestResult.info(e.getLocalizedMessage()).andRefresh();
		}
		
		return BaseRestResult.info("导入成功").andRefresh();
	}




	private ExcelType[] setExcel(String type) {
		ExcelType[] types=null;
		if("overdue".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//8
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//9
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//8
					new ExcelType(ExcelType.CELL_TYPE_DATE, true),//9
					new ExcelType(ExcelType.CELL_TYPE_DATE, true),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//11
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//12
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//13
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//14
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//15
			};
		}else if("payments".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//8
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//9
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//11
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//12
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//13
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//14
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//15
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//16
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//17
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//18
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//19
			};
		}else if("loanDetail".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//5
					new ExcelType(ExcelType.CELL_TYPE_DATE, true),//6
					new ExcelType(ExcelType.CELL_TYPE_DATE, true),//7
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//8
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//9
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//10
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//11
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//12
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//13
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//14
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//15
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//16
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//17
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//18
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//19
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//20
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//21
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//22
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//23
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//24
			};
		}else if("cmRdRelation".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//8
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//9
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//10
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//11
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//12
			};
		}else if("curLoanDetail".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//8
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//9
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//11
					new ExcelType(ExcelType.CELL_TYPE_DATE, true),//12
					new ExcelType(ExcelType.CELL_TYPE_DATE, true),//13
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//14
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//16
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//16
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//17
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//18
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//19
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//20
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//21
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//22
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//23
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//24
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//25
			};
		}
		//省区逾期率	
		else if("provinceOverdue".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//8
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//9
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//10
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//11
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//12
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//13
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//14
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//15
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//16
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//17
			};	
		}else if("curOverdue".equals(type)){ 
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//8
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//9
					new ExcelType(ExcelType.CELL_TYPE_LONG, false),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//11
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//12
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//13
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//14
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//15
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//16
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//17
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//18
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//19
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//20
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//21
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//22
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//23
			};	
		}else if("receiveOverdue".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//7
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//8
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//9
					new ExcelType(ExcelType.CELL_TYPE_LONG, false),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//11
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//12
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//13
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//14
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//15
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//16
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//17
					new ExcelType(ExcelType.CELL_TYPE_DATE, false),//18
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//19
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, false),//20
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//21
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//22
			};	
		}else if("validLoan".equals(type)){
			types = new ExcelType[] {//验证excel数据是否正确
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//1
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//2
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//3
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//4
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//5
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//6
					new ExcelType(ExcelType.CELL_TYPE_STRING, true),//7
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//8
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//9
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//10
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//11
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//12
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//13
					new ExcelType(ExcelType.CELL_TYPE_NUMERIC, true),//14
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//15
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//16
					new ExcelType(ExcelType.CELL_TYPE_STRING, false),//17
			};	
		}		
		return types;
	}
	
	
	
	
	
	
	
  	
  	

     
	//模板导出
	@RequestMapping("/download")
	public BaseRestResult<String> dowanload(HttpServletResponse httpServletResponse, HttpServletRequest request,String type) {
		BaseRestResult<String> result = null;
		String file = "";
		String outfile ="";
		if((type).equals("overdue")){
			 file = OverdualReportRestController.class.getResource("/excelTemplate/overdue_detail.xls").getPath();
			 outfile = "overdueReport.xls";
		}else if((type).equals("payments")){
			 file = OverdualReportRestController.class.getResource("/excelTemplate/received_payments_detail.xls").getPath();
			 outfile = "receivedPaymentsDetail.xls";
		}else if((type).equals("loanDetail")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/loan_detail.xls").getPath();
			 outfile = "放款详情导入模版.xls";
		}else if((type).equals("cmRdRelation")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/cm_rd_relation.xls").getPath();
			 outfile = "信贷经理与区域总监导入模版.xls";
		}else if((type).equals("curLoanDetail")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/cur_loan_detail.xls").getPath();
			 outfile = "当期投放导入模版.xls";
		}else if((type).equals("provinceOverdue")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/provinceOverdue_detail.xls").getPath();
			 outfile = "provinceOverdue_detail.xls";
		}else if((type).equals("validLoan")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/valid-loan.xls").getPath();
			 outfile = "有效投放总额导入模版.xls";
		}else if((type).equals("curOverdue")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/cur_overdue_amount.xls").getPath();
			 outfile = "cur_overdue_amount.xls";
		}else if((type).equals("receiveOverdue")) {
			file = OverdualReportRestController.class.getResource("/excelTemplate/receive_overdue_amount.xls").getPath();
			 outfile = "receive_overdue_amount.xls";
		}
		OutputStream os = null;
		try {
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(file);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
		return result;
	}
	
	
   public static void main(String[] args) {
	//test2();
	
	String str1 = new String("SEU") + new String("Calvin");        
	System.out.println(str1.intern() == str1);     
	System.out.println(str1 == "SEUCalvin");  
}
    private static void test2() {  
        List<String> testList = new ArrayList<>();  
        testList.add("6");  
        testList.add("a");  
        testList.add("8");  
        testList.add("9");  
        testList.add("10");  
          
        try {  
            for (String str : testList) {  
                int num = Integer.parseInt(str);  
                System.out.println(num);  
            }  
        } catch(NumberFormatException ex) {  
            System.out.println(ex);  
            throw new RuntimeException();
        }     
        System.out.println("22222");  
    }  
    
    
}
